<template>
  <!-- 一条文章单元格 -->
  <div>
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ artObj.title }}</span>
          <!-- 单图 -->
          <!-- <img
            class="thumb"
            v-if="artObj.cover.type === 1"
            :src="artObj.cover.images[0]"
            alt=""
          /> -->
          
          <van-image
            class="thumb"
            v-if="artObj.cover.type === 1"
            :src="artObj.cover.images[0]"
          >
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
        <!-- 多图 -->
        <div class="thumb_box" v-if="artObj.cover.type > 1">
          <!-- <img
            class="thumb"
            v-for="(imgUrl, index) in artObj.cover.images"
            :key="index"
            :src="imgUrl"
            alt=""
          /> -->
          <van-image
            class="thumb"
            v-for="(imgUrl, index) in artObj.cover.images"
            :key="index"
            :src="imgUrl"
          >
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ artObj.aut_name }}</span>
            <span>{{ artObj.comm_count }}评论</span>
            <span>{{ formatTime(artObj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="showInfo" v-if="isShow" />
        </div>
      </template>
    </van-cell>
    <!-- 反馈面板 -->
    <!-- 要挂载在 body上面，不然显示不出来 -->
    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      get-container="body"
      :cancel-text="bottomText"
      @cancel="cancelFn"
      @close="closeFn"
    />
  </div>
</template>

<script>
import { timeAgo } from "@/utils/date.js";
import { firstActions, secondActions } from "@/api/report.js";
export default {
  name: "ArticleItem",
  props: {
    artObj: Object,
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      show: false,
      actions: firstActions,
      bottomText: "取消",
    };
  },
  //使用utils里面的方法，给它进行注册
  methods: {
    formatTime: timeAgo,
    onSelect(item, action) {
      // 里面的item是 actions的内容，action是序列号
      // console.log(action);
      // console.log(item);
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      // this.show = false;
      if (item.name === "反馈垃圾内容") {
        this.actions = secondActions;
        this.bottomText = "返回";
        // this.show=true
      } else if (item.name === "不感兴趣") {
        this.$emit("dislikeEV", this.artObj.art_id);
        this.show = false;
      } else {
        this.$emit("reportEV", this.artObj.art_id, action.value);
        this.actions = firstActions;
        this.show = false;
      }
    },
    showInfo() {
      this.show = true;
    },
    //点击取消的函数
    //点击底部的返回进行判断，如果是返回，则展示一级面板，并且改底部文字为取消
    cancelFn() {
      if (this.bottomText === "返回") {
        this.show = true;
        this.actions = firstActions;
        this.bottomText = "取消";
      }
    },
    //二级面板时，点击遮罩层关闭，再次点击时，回到的时一级面板
    closeFn() {
      this.actions = firstActions;
      this.bottomText = "取消";
    },
  },
};
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
